#include "worker.h"
void * thread_func(void * arg){
    MYSQL*mysql=get_one_mysql_connect();
    /* const char perfix[]="/root/wangpan/thread_pool/server/files"; */
    thread_pool_info_t * thread_pool_info=(thread_pool_info_t*)arg;
    while(1){
        my_stack *each_stack=NULL;
        // get netfd old_netfd
        pthread_mutex_lock(&thread_pool_info->mutex);
        while(thread_pool_info->exit_flag==0&&thread_pool_info->netfd_queue.size<=0){
            pthread_cond_wait(&thread_pool_info->cond,&thread_pool_info->mutex);
        }
        printf("wake up!!!\n");
        if(thread_pool_info->exit_flag==1){
            printf("the server is going to quit ,I as a thread have quited!!\n");
            pthread_mutex_unlock(&thread_pool_info->mutex);
            pthread_exit(NULL);
        }
        int old_netfd;
        int recvn_ret=-1;
        train_t train;
        int netfd=get_head_netfd(&thread_pool_info->netfd_queue,&old_netfd);
        printf("I as a thread got a netfd :%d\n",netfd);
        del_head_netfd(&thread_pool_info->netfd_queue);
        int index= thread_pool_info->fd_to_index[old_netfd];
        each_stack=&thread_pool_info->dirStackArr[index];
        printf("dirStackArr.\n");
        print_stack(*each_stack);
        /* if(each_stack==NULL){ */
        /* printf("888888\n"); */
        /* } */
        pthread_mutex_unlock(&thread_pool_info->mutex);
        printf("unlock\n");
        printf("netfd = %d\n",netfd);

        bzero(&train,sizeof(train));
        char up_or_download_file_name[1024]={0};
        recvn_ret=recvn(netfd,&train.type,sizeof(train.type));
        printf("receive train.type:%d\n",train.type);

        if(train.type==CMD_GETS){
            printf("%%%%%%%%%%%%%%\n");
            char user_name[128]={0}; // 用户名//netfd->stacktop栈id->查表->username
            int vir_dire_id=each_stack->stack_top->id;                                                                             
            get_user_name_by_vid(mysql,vir_dire_id,user_name);
            int current_user_id=get_user_id_by_user_name(mysql,user_name);
            bzero(up_or_download_file_name,sizeof(up_or_download_file_name));
            recvn_ret=recvn(netfd,&train.length,sizeof(train.length));
            if(recvn_ret==-1){printf("netfd quit!%d\n",netfd);continue;}
            recvn_ret=recvn(netfd,train.data,train.length);
            if(recvn_ret==-1){printf("netfd quit!%d\n",netfd);continue;}
            strcpy(up_or_download_file_name,train.data);
            printf("--gets--%d,%s\n",train.length,up_or_download_file_name);
            user_t *user_dir=(user_t*)calloc(1,sizeof(user_t));
            getsfile(current_user_id,netfd,mysql,user_dir,up_or_download_file_name,each_stack);
            free(user_dir);
        }else if(train.type==CMD_PUTS){
            char user_name[128]={0}; // 用户名//netfd->stacktop栈id->查表->username
            int vir_dire_id=each_stack->stack_top->id;
            printf("vir_dire_id = %d\n",vir_dire_id);

            get_user_name_by_vid(mysql,vir_dire_id,user_name);
            printf("username = %s\n",user_name);
            
            int current_user_id=get_user_id_by_user_name(mysql,user_name);
            printf("current_user_id=%d\n",current_user_id);
            
            bzero(up_or_download_file_name,sizeof(up_or_download_file_name));
            recvn_ret=recvn(netfd,&train.length,sizeof(train.length));

            if(recvn_ret==-1){printf("netfd quit!%d\n",netfd);continue;}
            printf("receive train.length ret = %d\n", train.length);

            recvn_ret=recvn(netfd,train.data,train.length);
            if(recvn_ret==-1){printf("netfd quit!%d\n",netfd);continue;}
            printf("456\n");
            strcpy(up_or_download_file_name,train.data);
            printf("--puts--%d,%s\n",train.length,up_or_download_file_name);
            user_t *user_dir=(user_t*)calloc(1,sizeof(user_t));
            putsfile(current_user_id,netfd,mysql,user_dir,up_or_download_file_name,each_stack);
            free(user_dir);
        }        
        return NULL;
    }
}

//创建若干个工人线程
int create_worker(thread_pool_info_t *pthread_pool){                                                                                  
    for(int i=0;i<pthread_pool->worker_info.worker_num;i++){
        printf("id: %d thread has been created!\n",i+1);
        pthread_create(&pthread_pool->worker_info.worker_arr[i],NULL,thread_func,pthread_pool);
    }
    return 0;
}
